﻿using System;
using System.Collections.Generic;
using System.Web;

namespace IDAdmin.Lib.DataLayer
{
    public class ConnectionHelper
    {
        private const string CRYPT_KEY = "TRC9uZEpBDh9HvaeyOeh7";

        private static string _ConnectionString = "";
        /// <summary>
        /// Chuỗi tham số kết nối
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                try
                {
                    if (_ConnectionString == "")
                        _ConnectionString = Utils.CryptHelper.Decrypt(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"], CRYPT_KEY, true);
                    return _ConnectionString;
                }
                catch
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// Lấy đối tượng kết nối CSDL
        /// </summary>
        /// <returns></returns>
        public static System.Data.SqlClient.SqlConnection GetConnection()
        {
            try
            {
                System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection();
                cn.ConnectionString = ConnectionString;
                cn.Open();
                return cn;
            }
            catch
            {
                throw new Exception("Không kết nối được với máy chủ CSDL");
            }
        }
    }
}
